<?php

/**
 * This is the model class for table "{{area}}".
 *
 * The followings are the available columns in table '{{area}}':
 * @property integer $zar_id
 * @property integer $zat_id
 * @property integer $zar_topid
 * @property integer $zar_parentid
 * @property string $zar_childid
 * @property integer $zar_level
 * @property string $zar_title
 * @property string $zar_url
 * @property string $zar_alias_name
 * @property integer $zar_highlight
 * @property integer $zar_listorder
 * @property string $zar_description
 * @property integer $zar_status
 * @property integer $inputtime
 * @property integer $updatetime
 */
class Area extends CActiveRecord
{
	/**
	 * Returns the static model of the specified AR class.
	 * @param string $className active record class name.
	 * @return Area the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return '{{area}}';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('zat_id, zar_topid', 'required'),
			//array('zat_id, zar_topid, zar_parentid, zar_level, zar_highlight, zar_listorder, zar_status, inputtime, updatetime', 'numerical', 'integerOnly'=>true),
			array('zar_title, zar_url, zar_alias_name', 'length', 'max'=>255),
			array('zar_childid, zar_description', 'safe'),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('zar_id, zat_id, zar_topid, zar_parentid, zar_childid, zar_level, zar_title, zar_url, zar_alias_name, zar_highlight, zar_listorder, zar_description, zar_status, inputtime, updatetime', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'zar_id' => 'Zar',
			'zat_id' => 'Zat',
			'zar_topid' => 'Zar Topid',
			'zar_parentid' => 'Zar Parentid',
			'zar_childid' => 'Zar Childid',
			'zar_level' => 'Zar Level',
			'zar_title' => 'Zar Title',
			'zar_url' => 'Zar Url',
			'zar_alias_name' => 'Zar Alias Name',
			'zar_highlight' => 'Zar Highlight',
			'zar_listorder' => 'Zar Listorder',
			'zar_description' => 'Zar Description',
			'zar_status' => 'Zar Status',
			'inputtime' => 'Inputtime',
			'updatetime' => 'Updatetime',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('zar_id',$this->zar_id);
		$criteria->compare('zat_id',$this->zat_id);
		$criteria->compare('zar_topid',$this->zar_topid);
		$criteria->compare('zar_parentid',$this->zar_parentid);
		$criteria->compare('zar_childid',$this->zar_childid,true);
		$criteria->compare('zar_level',$this->zar_level);
		$criteria->compare('zar_title',$this->zar_title,true);
		$criteria->compare('zar_url',$this->zar_url,true);
		$criteria->compare('zar_alias_name',$this->zar_alias_name,true);
		$criteria->compare('zar_highlight',$this->zar_highlight);
		$criteria->compare('zar_listorder',$this->zar_listorder);
		$criteria->compare('zar_description',$this->zar_description,true);
		$criteria->compare('zar_status',$this->zar_status);
		$criteria->compare('inputtime',$this->inputtime);
		$criteria->compare('updatetime',$this->updatetime);

		$criteria->order = " inputtime DESC ";
		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}
// 	Area::getCity('MemAddres','1763','1786');
// 	echo $form->dropDownList($model,'zar_id',Area::getParentList('1786'),array('empty'=>'--请选择--'));

	/* ******************
	 * 查询前俩级联动下拉框
	 * $modelsname 模型名称
	 * $zar_id 最底级地域id
	 * $class 样式名称
	 ********************/
	public function getAreainfo($modelsname,$zar_id='',$class=''){	
		if(empty($zar_id)){
			self::getProvinceDropDown('','city_id',$class,$modelsname);		
			self::getCityDropDown('',$modelsname,'','city_id',$class);	
			echo CHtml::dropDownList($modelsname.'[zar_id]','',array('empty'=>'--请选择--',),array('class'=>$class));
		}else{
	        $parentid = self::getParentInfoByid($zar_id)->zar_parentid;
	        if(!empty($parentid)){
	        	$topid    = self::getParentInfoByid($parentid)->zar_parentid;
	        }
			//如果传进来的地域id是地区级，显示三个下拉框
			//否则显示俩个下拉框
// 			if(!empty($topid)){
				self::getProvinceDropDown($topid,'city_id',$class,$modelsname);
				self::getCityDropDown($topid,$modelsname,$parentid,'city_id',$class);
				echo CHtml::dropDownList($modelsname.'[zar_id]',$zar_id,Area::getParentList($parentid),array('class'=>$class));
// 			}else{
// 				self::getProvinceDropDown($parentid,$modelsname.'_zar_id',$class);
// 				self::getCityDropDown($parentid,$modelsname,$zar_id,$modelsname.'_zar_id',$class);
// 			}
		}
	}
	/*查询省级的地域下拉框  * 
	 *  $modelsname 要录入数据库的模型名称
	 *  $selectid 下拉框选中id
	 *  $class 样式名称
	 *  $field 字段名称*/
	public function getProvinceDropDown($selectid='',$field='city_id',$class='',$modelsname=''){
		echo CHtml::dropDownList('province_id',$selectid, Area::getParentList(),array(
				'empty'=>'--请选择--',
				'ajax' => array(
						'type'=>'GET',
						'url'=>Yii::app()->createUrl('ajax/dynamiccitys'),
						'update'=>'#'.$field,
						'data'=>array('zar_parentid'=>'js:this.value'),
				),
				'class'=>$class
		));
	}
	/*查询市级地域下拉框 
	 *  $modelsname 要录入数据库的模型名称
	 *  $selectid 下拉框选中id
	 *  $class 样式名称
	 *  $field 字段名称*/
	public function getCityDropDown($topid='',$modelsname='',$selectid='',$field='city_id',$class=''){
		if(!empty($topid)){
			$citydata = Area::getParentList($topid);
		}else{
			$citydata=array();
		}
		echo CHtml::dropDownList($field,$selectid,$citydata,array(
				'empty'=>'--请选择--',
				'ajax' => array(
						'type'=>'GET',
						'url'=>Yii::app()->createUrl('ajax/dynamiccitys'),
						'update'=>'#'.$modelsname.'_zar_id',
						'data'=>array('zar_parentid'=>'js:this.value'),
				),
				'class'=>$class
		));
	}
	
	
	/*************************************
	 * 查找 某个地址的父级信息
	 *@param $filed 一个字符串，或者用逗号隔开
	 *************************************/
	public function getParentInfoByid($zar_id,$filed='zar_parentid'){
		$criteria =  new CDbCriteria;
		$criteria ->select=$filed;
		$criteria ->condition='zar_id=:zar_id';
		$criteria ->params=array(':zar_id'=>$zar_id);
		return Area::model()->find($criteria);		
	}

	/*************************************
	 * 查找 某个地址的子项信息
	*@param $parendid
	*************************************/
	public function getChildidInfo($parentid){
		$model = self::model()->findAllByAttributes(array('zar_parentid'=>$parentid));
		return $model;
	}
	/************************
	 * 查找某一级下面的全部子项
	 *$parentid 父级id
	 ***********************/
	public function getParentList($parentid='0')
	{
		$model = self::model()->findAllByAttributes(array('zar_parentid'=>$parentid));
		return CHtml::listData($model, 'zar_id', 'zar_title');
	}
	
	/************************
	 * 根据最低一级id返回完整省市县信息
	 * $id 最低一级id
	 * $return 数组
	 ***********************/
	public function getFullAreaByFloorId($id)
	{
		$model = self::model()->findByPk($id);
		$area = array();
		if($model){
			$area1 = $model->attributes;
			$area['area1'] = $area1;
			$model = $model->getParentInfoByid($area1['zar_parentid'],'*');
			if($model){
				$area2 = $model->attributes;
				$area['area2'] = $area2;
				$model = $model->getParentInfoByid($area2['zar_parentid'],'*');
				if($model){
					$area3 = $model->attributes;
					$area['area3'] = $area3;
				}
			}
		}
		return $area;
	}
	
	/************************
	 * 根据最低一级id返回完整省市县信息
	 * $id 最低一级id
	 * $return 字符串
	 ***********************/
	public static function getAreaStrByFloorId($id)
	{
		$model = self::model()->findByPk($id);
		$str = '';
		if($model){
			$area1 = $model->attributes;
			$str .= $area1['zar_title'];
			$model = $model->getParentInfoByid($area1['zar_parentid'],'*');
			if($model){
				$area2 = $model->attributes;
				$str = $area2['zar_title'].$str;
				$model = $model->getParentInfoByid($area2['zar_parentid'],'*');
				if($model){
					$area3 = $model->attributes;
					$str = $area3['zar_title'].$str;
				}
			}
		}
		return $str;
	}

	/**
	 * 获取运送地区
	 */
	public function getAreas($areaid){
		$criteria = new CDbCriteria;
		$criteria->compare('zar_id',explode(',',$areaid));
		$result = $this->findAll($criteria);
		$data = CHtml::ListData($result,'zar_id','zar_title');
		return join(',',$data);
	}
	
}